# -*- coding: UTF-8 -*-
import requests

adcode = 100000
lastCode = 0


def writeFile(filepath, data):
    with open(filepath, 'w', encoding='utf-8') as wf:
        wf.write(data)


def reqAndSave(code):
    global lastCode
    if lastCode == code:
        return
    lastCode = code
    url = "https://geo.datav.aliyun.com/areas_v2/bound/" + \
        str(code) + "_full.json"
    res = requests.get(url)
    resJson = res.json()
    sStr = str(resJson)
    # 替换单引号
    dStr = sStr.replace("'", '"')
    # 替换None
    dStr = dStr.replace('None', 'null')
    #写文件
    writeFile("./geojson/" + str(code) + "_full.json", dStr)
    print(code)
    for feature in resJson['features']:
        if feature['properties']['level'] != 'district':
            reqAndSave(feature['properties']['adcode'])


if __name__ == "__main__":
    reqAndSave(adcode)
